<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
	public function __construct($application) {
		parent::__construct($application);
		require_once 'Zend/Loader/Autoloader.php';
		$loader = Zend_Loader_Autoloader::getInstance();
		$loader->registerNamespace(array('Controller', 'Model', 'Vendor'))
			->suppressNotFoundWarnings(true)
			->setFallbackAutoloader(false);

		//加载帮助方法
		$loader->pushAutoloader(array($this, 'loadHelper'), 'Helper_');

		//加载cron job 所需模型
		$loader->pushAutoloader(array($this, 'loadCronJobModel'), 'Cron_Model_');

		//加载自定义(无"命名空间"前缀)项目类库
		$loader->pushAutoloader(array($this, 'loadExtend'));

		Requests::register_autoloader();
	}

	/**
	 * 加载帮助方法
	 * @param  [type] $class [description]
	 * @return [type]        [description]
	 */
	public function loadHelper($class) {
		$fileName = str_replace("Helper_", "", $class);
		$filePath = APPLICATION_PATH . DIRECTORY_SEPARATOR . "helpers" . DIRECTORY_SEPARATOR . $fileName . ".php";
		if (file_exists($filePath)) {
			require_once $filePath;
		}
	}

	/**
	 * 加载自定义类库
	 * @param  [type] $class [description]
	 * @return [type]        [description]
	 */
	public function loadExtend($class) {
		$filename = LIBRARY_PATH . DIRECTORY_SEPARATOR . $class . DIRECTORY_SEPARATOR . $class . '.php';
		if (!class_exists($class) && file_exists($filename)) {
			require_once $filename;
		}
	}

	public function loadCronJobModel($class) {
		$classPath = str_replace('Cron_Model_', '', $class);
		$classPath = str_replace('_', DIRECTORY_SEPARATOR, $classPath);
		$filePath = CRONJOB_PATH . DIRECTORY_SEPARATOR . 'models' . DIRECTORY_SEPARATOR . $classPath . '.php';
		if (file_exists($filePath)) {
			require_once $filePath;
		}
	}
}